package plutuscat.shishicai.service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import plutuscat.shishicai.dao.LotteryDao;
import plutuscat.shishicai.domain.Lottery;
import plutuscat.shishicai.util.MyDateUtil;

/**
*
* @author dw_chenqi
* @date 2016年5月3日下午1:15:05
*/
@Service
public class LotteryService {

	private static final Logger log = LoggerFactory.getLogger(LotteryService.class);

	@Autowired
	private LotteryDao lotteryDao;

	/**
	 * 同步最新的彩票的数据
	 */
	public void syncLotteryData() {
		log.info("同步彩票数据!");
		Set<Lottery> lotterySet = new TreeSet<Lottery>(new Lottery.LotteryComparator());
		if (isNeedPullHistoryData()) {
			//同步历史数据
			log.info("下载彩票的历史数据并入库!");
			lotterySet = lotteryDao.pullHistoryLotteryData();
			String today = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
			lotterySet.addAll(lotteryDao.pullLotteryData(today));
		} else {
			//同步最近一段时间不完整的彩票数据
			for (String date : getSyncDates()) {
				lotterySet.addAll(lotteryDao.pullLotteryData(date));
			}
		}

		addLotterys(lotterySet);
	}

	private List<String> getSyncDates() {
		List<String> dates = lotteryDao.getSyncDates();
		String maxDateInDates = null;
		try {
			if (CollectionUtils.isEmpty(dates)) {//查询最大日期,之前的数据都是完整的
				String maxDate = lotteryDao.getMaxDate();
				if (StringUtils.isBlank(maxDate)) {//为空,表示表里数据为空
					Date md = DateUtils.parseDate(maxDate, "yyyy-MM-dd");
					maxDateInDates = DateFormatUtils.format(DateUtils.addDays(md, 1), "yyyy-MM-dd");
				}
			} else {
				maxDateInDates = new TreeSet<String>(dates).last();
			}

			// 表里已有最大日期到今天日期的数据,maybe 中间有间断
			Date maxDate = DateUtils.parseDate(maxDateInDates, "yyyy-MM-dd");
			int diff = MyDateUtil.diffDay(maxDate, new Date());
			for (int i = 0; diff > 0 && i < diff; i++) {
				dates.add(DateFormatUtils.format(DateUtils.addDays(maxDate, (i + 1)), "yyyy-MM-dd"));
			}
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		return dates;
	}

	private boolean isNeedPullHistoryData() {
		Map<String, Object> map = lotteryDao.getMaxDateAndCount();
		if (CollectionUtils.isEmpty(map)) {
			return true;
		}
		if (Integer.parseInt(map.get("ct") + "") == 0)
			return true;
		try {
			Date maxDate = DateUtils.parseDate(map.get("maxDate") + "", "yyyy-MM-dd");
			return MyDateUtil.diffDay(new Date(), maxDate) > 30;//超过30天,则同步数据
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	public void addLotterys(Collection<Lottery> lotteryList) {
		//lotteryDao.addLotterys(lotteryList);
		lotteryDao.batchAdd(new ArrayList(lotteryList));
	}

}
